import store from '@/store'
// import {getTokenFailure} from '@/api/other'       // 验证token

const authorization = (overdue = false) => {
  return new Promise((resolve, reject) => {
    if (store.state.vuex_user.token && !overdue) {
      uni.$cu.showLoading();
      // 调用检测是否token过期接口
      getTokenFailure({}).then(() => {
        uni.hideLoading();
        resolve();
      })
    } else {
      let currentPages = getCurrentPages()[getCurrentPages().length - 1];
      let fullPath = encodeURIComponent(currentPages['$page']['fullPath']);
      
      if (currentPages.route == '/pages/authorize/index') return reject();
      if (overdue) {
        uni.redirectTo({url: `/pages/authorize/index?overdue=${overdue}&from=${fullPath}`})
      } else {
        uni.navigateTo({url: `/pages/authorize/index?overdue=${overdue}&from=${fullPath}`})
      }
      reject();
    }
  })
}

export default authorization
